﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Dao;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.Utils;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.LogTool;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace CastingWellEquipmentGUI.ViewModel
{
    public class SystemSettingViewModel : ViewModelBase
    {

        private string _DataBaseIpAddress;

        public string DataBaseIpAddress
        {
            get { return _DataBaseIpAddress; }
            set
            {
                _DataBaseIpAddress = value;
                RaisePropertyChanged("DataBaseIpAddress");
            }
        }

        private string _CurrentlySelectedItem;
        public string CurrentlySelectedItem
        {
            get { return _CurrentlySelectedItem; }
            set
            {
                _CurrentlySelectedItem = value;
                RaisePropertyChanged("CurrentlySelectedItem");
            }
        }

        /// <summary>
        /// 下拉框列表
        /// </summary>
        private ObservableCollection<string> _ComBoxItems;
        public ObservableCollection<string> ComBoxItems
        {
            get
            {
                return _ComBoxItems;
            }
            set
            {
                _ComBoxItems = value;
                RaisePropertyChanged("ComBoxItems");
            }
        }

        /// <summary>
        /// 保存命令
        /// </summary>
        public DelegateCommand SaveCommand { get; set; }

        public SystemSettingViewModel()
        {
            ComBoxItems = new ObservableCollection<string>
            {
                "扁锭",
                "圆棒"
            };
            DataBaseIpAddress = SystemConfig.dataBaseIpAddress;
            CurrentlySelectedItem = SystemConfig.equipmentCategory;

            SaveCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(SaveHandler)
            };
        }
        /// <summary>
        /// 保存处理器
        /// </summary>
        /// <param name="obj"></param>
        private void SaveHandler(object obj)
        {

            if (DataBaseIpAddress == null || DataBaseIpAddress.Trim().Length == 0)
            {
                MessageBox.Show("必须输入数据库IP地址，格式IPV4！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (CurrentlySelectedItem == null || CurrentlySelectedItem.Trim().Length == 0)
            {
                MessageBox.Show("必须选择设备类型！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                if (XyhUtils.IsHasSQLInject(DataBaseIpAddress))
                {
                    MessageBox.Show("数据库IP地址不可使用包含SQL注入关键词进行保存操作！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else if (Regex.IsMatch(DataBaseIpAddress, "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$") ||
                    Regex.IsMatch(DataBaseIpAddress, "^([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)|::([\\da−fA−F]1,4:)0,4((25[0−5]|2[0−4]\\d|[01]?\\d\\d?)\\.)3(25[0−5]|2[0−4]\\d|[01]?\\d\\d?)|::([\\da−fA−F]1,4:)0,4((25[0−5]|2[0−4]\\d|[01]?\\d\\d?)\\.)3(25[0−5]|2[0−4]\\d|[01]?\\d\\d?)|^([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)|([\\da−fA−F]1,4:)2:([\\da−fA−F]1,4:)0,2((25[0−5]|2[0−4]\\d|[01]?\\d\\d?)\\.)3(25[0−5]|2[0−4]\\d|[01]?\\d\\d?)|([\\da−fA−F]1,4:)2:([\\da−fA−F]1,4:)0,2((25[0−5]|2[0−4]\\d|[01]?\\d\\d?)\\.)3(25[0−5]|2[0−4]\\d|[01]?\\d\\d?)|^([\\da-fA-F]{1,4}:){3}:([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)|([\\da−fA−F]1,4:)4:((25[0−5]|2[0−4]\\d|[01]?\\d\\d?)\\.)3(25[0−5]|2[0−4]\\d|[01]?\\d\\d?)|([\\da−fA−F]1,4:)4:((25[0−5]|2[0−4]\\d|[01]?\\d\\d?)\\.)3(25[0−5]|2[0−4]\\d|[01]?\\d\\d?)|^([\\da-fA-F]{1,4}:){7}[\\da-fA-F]{1,4}|:((:[\\da−fA−F]1,4)1,6|:)|:((:[\\da−fA−F]1,4)1,6|:)|^[\\da-fA-F]{1,4}:((:[\\da-fA-F]{1,4}){1,5}|:)|([\\da−fA−F]1,4:)2((:[\\da−fA−F]1,4)1,4|:)|([\\da−fA−F]1,4:)2((:[\\da−fA−F]1,4)1,4|:)|^([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1,4}){1,3}|:)|([\\da−fA−F]1,4:)4((:[\\da−fA−F]1,4)1,2|:)|([\\da−fA−F]1,4:)4((:[\\da−fA−F]1,4)1,2|:)|^([\\da-fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})?|([\\da−fA−F]1,4:)6:|([\\da−fA−F]1,4:)6:"))
                {
                    string before = SystemConfig.dataBaseIpAddress;
                    try
                    {

                        SystemConfig.dataBaseIpAddress = DataBaseIpAddress;
                        string sql = "select * from [User] where Account = @Account;";
                        User VerificationUser = new User
                        {
                            Account = "Admin"
                        };
                        User DataBaseUser = DapperToolsThisSystem.Query<User>(sql, VerificationUser);
                        if (DataBaseUser != null)
                        {
                            string xmlFilePath = SystemConfig.xmlFilePath;
                            if (File.Exists(xmlFilePath))
                            {
                                File.Delete(xmlFilePath);
                            }

                            XmlHelper applicationConfig = new XmlHelper();
                            applicationConfig.CreateXmlDocument("ApplicationConfig.xml", "ApplicationConfig", "utf-8");
                            //数据库IP地址
                            applicationConfig.InsertNode(xmlFilePath,
                                                         "DataBaseIpAddress",
                                                         true,
                                                         "ApplicationConfig",
                                                         new Hashtable
                                                         {
                                                { "value", DataBaseIpAddress }
                                                         },
                                                         null);
                            //设备类别
                            applicationConfig.InsertNode(xmlFilePath,
                                                         "EquipmentCategory",
                                                         true,
                                                         "ApplicationConfig",
                                                         new Hashtable
                                                         {
                                                { "value", CurrentlySelectedItem }
                                                         },
                                                         null);
                            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideSystemSetting"));
                            MessageBox.Show("保存成功！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            SystemConfig.dataBaseIpAddress = before;
                            MessageBox.Show("数据库IP地址不正确！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        LogTool.SaveSysLog(e.Message,"连接数据库");
                        SystemConfig.dataBaseIpAddress = before;
                        MessageBox.Show("数据库IP地址不正确！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                }
                else
                {
                    MessageBox.Show("数据库IP地址格式不正确！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
        }
    }
}
